package com.xuetang9.fujian.studentdemo.daoimpl;

import com.xuetang9.fujian.studentdemo.dao.StudentDao;
import com.xuetang9.fujian.studentdemo.entity.Student;
import com.xuetang9.fujian.studentdemo.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: fujian
 * @description:
 * @date: 2021/5/18 15:54
 * @version: V1.0
 * @package: com.xuetang9.fujian.studentdemo.daoimpl
 **/
public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public List<Student> queryByName(String name) {
        List<Student> studentList = new ArrayList<>();
        String sql = "select * from students where stuName=?";
        Object[] params = {name};
        objList2StuList(studentList, sql, params);
        return studentList;
    }

    private void objList2StuList(List<Student> studentList, String sql, Object[] params) {
        List<Object> objects = executeQuery(sql, params);
        if (Objects.nonNull(objects) && objects.size() !=0 ){
            for (Object object : objects) {
                studentList.add((Student) object);
            }
        }
    }

    @Override
    public boolean save(Student student) {
        String sql = null;
        if (student.getStuId()==0)
            sql = "insert into students values(default,?,?,?,?,?,?,?,?,now());";
        else
            sql = String.format("update students set StuName=?,LoginPwd=?,Sex=?,GradeID=?,Mobile=?,Address=?,Email=?,IdentifyCard=?,CreateDate=now() where StuId=%d;",student.getStuId());
        boolean isSuccess = false;
        try {
            preparedStatement = GetConnection().prepareStatement(sql);
            preparedStatement.setString(1,student.getStuName());
            preparedStatement.setString(2,student.getLoginPwd());
            preparedStatement.setString(3,student.getSex());
            preparedStatement.setInt(4,student.getStuId());
            preparedStatement.setString(5,student.getMobile());
            preparedStatement.setString(6,student.getAddress());
            preparedStatement.setString(7,student.getEmail());
            preparedStatement.setString(8,student.getIdentityCard());
           isSuccess = preparedStatement.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(resultSet,statement,preparedStatement,connection);
        }
        return !isSuccess;
    }

    @Override
    public boolean delete(int id) {
        String sql = "delete from students where StuID=?;";
        boolean isSuccess = false;
        try {
            preparedStatement = GetConnection().prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.execute();
            isSuccess = true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(resultSet,statement,preparedStatement,connection);
        }
        return isSuccess;
    }

    @Override
    public boolean update(Student student) {
        return save(student);
    }

    @Override
    public Student queryById(int id) {
        String sql = "select * from students where StuId=?;";
        List<Object> objectList = executeQuery(sql, id);
        if (Objects.nonNull(objectList) && objectList.size() != 0){
            return (Student) objectList.get(0);
        }
        return null;
    }

    @Override
    public List<Student> queryAll() {
        List<Student> studentList = new ArrayList<>();
        String sql = "select * from students;";
        objList2StuList(studentList,sql,null);
        return studentList;
    }

    @Override
    public List<Object> executeQuery(String sql, Object... params) {
        return super.executeQuery(sql, params);
    }

    @Override
    public void fillList(List<Object> objList, ResultSet resultSet) throws SQLException {
        while (resultSet.next()){
            Student student = new Student();
            // 填充结果集中的数据
            student.setStuId(resultSet.getInt("stuId"));
            student.setStuName(resultSet.getString("stuName"));
            student.setLoginPwd(resultSet.getString("loginPwd"));
            student.setAddress(resultSet.getString("address"));
            student.setSex(resultSet.getString("sex"));
            student.setCreatDate(resultSet.getDate("createDate"));
            student.setEmail(resultSet.getString("email"));
            student.setGradeId(resultSet.getInt("gradeId"));
            student.setIdentityCard(resultSet.getString("identifyCard"));
            student.setMobile(resultSet.getString("mobile"));
            objList.add(student);
        }
    }
}
